home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
- /*
- * pscat -
- * Concatenate PostScript files for book
- * generation.
- *
- * To compile:
- * cc pscat.c -o pscat
- *
- * Paul Haeberli - 1993
- */
- #include "stdio.h"
- #include "math.h"
- #include "ctype.h"
-
- #define INBUFSIZE 4096
- #define MAXPAGES 1000
-
- char buf[INBUFSIZE];
- int secstart[MAXPAGES];
- int secend[MAXPAGES];
- int trailstart;
- int trailend;
-
- sizeoffile(f)
- FILE *f;
- {
- int pos, ret;
-
- pos = ftell(f);
- if ((ret = fseek(f,0,2)) < 0) {
- fprintf(stderr,"sizeoffile: seek error\n");
- exit(1);
- }
- ret = ftell(f);
- if (fseek(f,pos,0) < 0) {
- fprintf(stderr,"sizeoffile: seek error\n");
- exit(1);
- }
- return ret;
- }
-
- indexpages(inf)
- FILE *inf;
- {
- int i, n, before, after;
-
- n = 0;
- trailstart = 0;
- trailend = 0;
- while(fgets(buf,INBUFSIZE,inf)) {
- if(strncmp(buf,"%%Page:",7) == 0) {
- after = ftell(inf);
- if(n>0)
- secend[n-1] = before;
- secstart[n] = after;
- n++;
- } else if(strncmp(buf,"%%Trailer",9) == 0) {
- after = ftell(inf);
- if(n>0)
- secend[n-1] = before;
- trailstart = after;
- trailend = sizeoffile(inf);
- return n;
- }
- before = ftell(inf);
- }
- secend[n-1] = ftell(inf);
- return n;
- }
-
- putpage(inf,pageno,npages)
- FILE *inf;
- int pageno, npages;
- {
- int nbytes;
-
- if(pageno<npages && pageno>=0) {
- nbytes = secend[pageno]-secstart[pageno];
- putsection(inf,secstart[pageno],nbytes);
- }
- }
-
- putsection(inf,start,nbytes)
- FILE *inf;
- int start, nbytes;
- {
- int nr;
-
- fseek(inf,start,0);
- while(nbytes>0) {
- if(!fgets(buf,INBUFSIZE,inf)) {
- fprintf(stderr,"pscat: bad read in putsection\n");
- exit(1);
- }
- nr = strlen(buf);
- nbytes -= nr;
- if(buf[0] != '%')
- fputs(buf,stdout);
- }
- if(nbytes != 0) {
- fprintf(stderr,"pscat: strange read in putsection\n");
- exit(1);
- }
- }
-
- main(argc,argv)
- int argc;
- char **argv;
- {
- FILE *inf;
- int npages, i, outpage;
- float xoff, yoff, scale;
- char *infile;
-
- if(argc<5) {
- fprintf(stderr,"\nusage: pscat in.ps xoff yoff scale\n");
- fprintf(stderr," [in2.ps xoff yoff scale] ... > out.ps\n");
- fprintf(stderr,"\n");
- exit(1);
- }
- printf("%%!PS-Adobe-2.0\n");
- printf("%%%%EndComments\n");
- outpage = 1;
- i = 1;
- for(i=1; i<argc-3; i+=4) {
- infile = argv[i+0];
- xoff = atof(argv[i+1]);
- yoff = atof(argv[i+2]);
- scale = atof(argv[i+3]);
- inf = fopen(infile,"r");
- if(!inf) {
- fprintf(stderr,"pscat: can't open %s\n",argv[1]);
- exit(1);
- }
- npages = indexpages(inf);
- fprintf(stderr,"Document pages %d\n",npages);
-
- if(npages == 0) {
- secend[0] = sizeoffile(inf);
- secstart[0] = 0;
- npages = 1;
- }
- putpages(inf,npages,outpage,xoff,yoff,scale);
- outpage += npages;
- fclose(inf);
- }
- exit(0);
- }
-
- putpages(inf,npages,outpage,xoff,yoff,scale)
- FILE *inf;
- int npages, outpage;
- float xoff, yoff, scale;
- {
- int pageno;
-
- for(pageno=0; pageno<npages; pageno++) {
- printf("%%%%Page: page%d %d\n",outpage+pageno,outpage+pageno);
- dosave(inf);
- printf("%f %f scale\n",scale,scale);
- printf("%f %f translate\n",72.0*xoff,72.0*yoff);
- putsection(inf,0,secstart[0]);
- putpage(inf,pageno,npages);
- dorestore(inf);
- }
- printf("%%%%EOF\n");
- }
-
- dosave(inf)
- FILE *inf;
- {
- printf("gsave\n");
- }
-
- dorestore(inf)
- FILE *inf;
- {
- if(trailstart)
- putsection(inf,trailstart,trailend-trailstart);
- printf("grestore\n");
- }
-